<template>
  <el-dialog
    title="订单发货"
    :visible.sync="dialogVisible"
    width="98%">
    <div>
      <el-form ref="form" :model="expressForm" label-width="80px" size="small">
        <el-form-item label="物流单号:">
          <el-input style="width: 90%;" v-model="expressForm.expressNumber"></el-input>
        </el-form-item>
        <el-form-item label="物流公司:">
          <el-select style="width: 60%;" v-model="expressForm.expressCompany"
                     placeholder="请选择 物流公司">
            <el-option :label="reason" :value="reason" v-for="(reason,index) in expressCompanyArr"
                       :key="index"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确认选择</el-button>
    </div>
  </el-dialog>

</template>

<script>
import AddressItem from './AddressItem'
export default {
  name: 'OrderApplyStartSendDialog',
  components: {AddressItem},
  data: function () {
    return {
      isGet:false,
      callback:null,
      dialogVisible:false,
      expressForm: {
        // 退回类型
        expressType: 0,
        expressCompany: '',
        expressNumber: '',
      },
      expressCompanyArr: dict.ExpressCompanyList,
      orderApply:{}
    }
  },
  watch:{
    $route:function () {
      this.dialogVisible=false
      this.$forceUpdate()
    },
    dialogVisible:function () {
      if (!this.dialogVisible){
        this.expressForm.expressCompany=''
        this.expressForm.expressNumber=''
      }
    }
  },
  mounted () {
    let $this = this
    $this.bus.$on('OrderApplyStartSendDialogCallBus', function (orderApply,callback) {
      $this.dialogVisible = true
      $this.callback=callback
      $this.orderApply=orderApply
      $this.$forceUpdate()
    })

  },
  methods: {
    // 打开弹窗
    open(order,callback){
      this.dialogVisible = true
      this.callback=callback
      this.orderApply=orderApply
      this.$forceUpdate()
    },
    // 提交信息
    submit: function () {
      let $this = this
      if (!$this.expressForm.expressNumber) {
        return $this.$message.error('快递单号 不能为空')
      }
      if (!$this.checkExpressNumber($this.expressForm.expressNumber)){
        return $this.$message.error('快递单号 必须是由(A-Za-z0-9)组成，长度在5~50位之间')
      }
      if (!$this.expressForm.expressCompany) {
        return $this.$message.error('快递公司 不能为空')
      }
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var form = {
            id: $this.orderApply.id,
            expressCompany: $this.expressForm.expressCompany,
            expressType: $this.expressForm.expressType,
            expressNumber: $this.expressForm.expressNumber,
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameShop + 'OrderApply/submitExpress', form, function () {
            $this.$message.success('操作成功')
            $this.currLoading = false
            $this.dialogVisible = false
            $this.callback()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
